// src/stores/excalidrawStore.ts
import { defineStore } from "pinia";
import { ref } from "vue";

export const useExcalidrawStore = defineStore(
  "excalidraw",
  () => {
    // 当前房间的绘图数据
    const drawings = ref<Record<string, any>>({});
    const currentDrawing = ref<any>(null);
    const currentRoomId = ref<string>("");
    const setCurrentRoomId = (roomId: string) => {
      currentRoomId.value = roomId;
      currentDrawing.value = drawings.value[roomId] || null;
    };
    const setCurrentDrawing = (roomId: string, data: any) => {
      drawings.value[roomId] = data;
      currentDrawing.value = data;
    };

    const getDrawing = (roomId: string) => {
      currentDrawing.value = drawings.value[roomId] || null;
      return currentDrawing.value;
    };

    const clearDrawing = (roomId: string) => {
      delete drawings.value[roomId];
      currentDrawing.value = null;
    };

    return {
      currentRoomId,
      drawings,
      currentDrawing,
      setCurrentRoomId,
      setCurrentDrawing,
      getDrawing,
      clearDrawing,
    };
  },
  {
    persist: true,
  }
);
